package com.xm.servlet.reception_desk.redeem_codes;

import com.xm.util.DecryptExampleUtil;
import lombok.SneakyThrows;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;


/**
 * F码通道
 * @author 吴添文
 * @date 2023年12月22日
 *
 */
@WebServlet("/redeem/codes")
public class RedeemCodesServlet extends HttpServlet {
    @SneakyThrows(Exception.class)
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        //获取所有参数 并且封装在Map集合
        Map<String, String[]> parameterMap = req.getParameterMap();
        HttpSession session = req.getSession(false);
        // 获取会话中存储的CSRF令牌
        String sessionToken = null;
        try {
            sessionToken = (String) session.getAttribute("token");
        }catch (Exception e){
            System.out.println("没有登陆");
        }
        //AES解密
        String informationId = DecryptExampleUtil.decrypt(parameterMap.get("informationId")[0].trim());
        System.out.println("解密结果：" + informationId);
        // 验证CSRF令牌
        if (sessionToken != null) {
            req.setAttribute("accountId", parameterMap.get("accountId")[0]);
            req.setAttribute("name", parameterMap.get("name")[0]);
            req.setAttribute("id",Integer.parseInt(informationId));
            // 令牌失效
            req.setAttribute("status", true);
        }else{
            System.out.println(parameterMap.get("accountId")[0]+"----");
            req.setAttribute("accountId", "0");
            req.setAttribute("name", "null");
            req.setAttribute("id", -1);
            // 令牌失效
            req.setAttribute("status", false);
            System.out.println("CSRF令牌验证失败");
        }
        req.getRequestDispatcher("/WEB-INF/page/reception_desk/redeem_codes.jsp").forward(req, resp);
    }
}
